package com.example.bobo.yuejian322.Fragment;


import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.TextView;

import com.example.bobo.yuejian322.Fragment.Peixun.Peixun_CainiaoFragment;
import com.example.bobo.yuejian322.Fragment.Peixun.Peixun_JinjieFragment;
import com.example.bobo.yuejian322.Fragment.Peixun.Peixun_QuanbuFragment;
import com.example.bobo.yuejian322.Fragment.Peixun.Peixun_RumenFragment;
import com.example.bobo.yuejian322.MyImage.banners.BannerView;
import com.example.bobo.yuejian322.R;
import com.example.bobo.yuejian322.base.BaseFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class PeixunFragment extends BaseFragment implements View.OnClickListener {

    private TextView rumen, cainiao, jinjie, quanbu;
    private FragmentTransaction transaction;
    private FragmentManager manager;
    private Peixun_CainiaoFragment peixun_cainiaoFragment;
    private Peixun_JinjieFragment peixun_jinjieFragment;
    private Peixun_QuanbuFragment peixun_quanbuFragment;
    private Peixun_RumenFragment peixun_rumenFragment;



    private int[] imgs = {R.drawable.image1,R.drawable.image2,R.drawable.image3,R.drawable.image4};
    private List<View> viewList;
    BannerView bannerView;

    public PeixunFragment() {
        // Required empty public constructor
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        rumen = (TextView) view.findViewById(R.id.rumen);
        cainiao = (TextView) view.findViewById(R.id.cainiao);
        jinjie = (TextView) view.findViewById(R.id.jinjie);
        quanbu = (TextView) view.findViewById(R.id.quanbu);
        bannerView = (BannerView) view.findViewById(R.id.image_peixun);
        init();
        manager = getChildFragmentManager();
        setSelect(0);
        lunbotu();
    }
    private void init(){
        rumen.setOnClickListener(this);
        cainiao.setOnClickListener(this);
        jinjie.setOnClickListener(this);
        quanbu.setOnClickListener(this);
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_peixun, container, false);
    }
    private void setSelect(int i)
    {
        FragmentManager fm =  getChildFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        hideFragment(transaction);
        color();
        switch (i) {
            case 0:
                if (peixun_rumenFragment == null) {
                    peixun_rumenFragment = new Peixun_RumenFragment();
                    transaction.add(R.id.peixun_FrameLayout, peixun_rumenFragment);
                } else {
                    transaction.show(peixun_rumenFragment);
                }
                rumen.setTextColor(this.getResources().getColor(R.color.green));
                rumen.setBackgroundDrawable(this.getResources().getDrawable(R.drawable.unshape_textview));
                break;
            case 1:
                if (peixun_cainiaoFragment == null) {
                    peixun_cainiaoFragment = new Peixun_CainiaoFragment();
                    transaction.add(R.id.peixun_FrameLayout, peixun_cainiaoFragment);
                } else {
                    transaction.show(peixun_cainiaoFragment);
                }
                cainiao.setTextColor(this.getResources().getColor(R.color.green));
                cainiao.setBackgroundDrawable(this.getResources().getDrawable(R.drawable.unshape_textview));
                break;
            case 2:
                if (peixun_jinjieFragment == null) {
                    peixun_jinjieFragment = new Peixun_JinjieFragment();
                    transaction.add(R.id.peixun_FrameLayout, peixun_jinjieFragment);
                } else {
                    transaction.show(peixun_jinjieFragment);
                }
                jinjie.setTextColor(this.getResources().getColor(R.color.green));
                jinjie.setBackgroundDrawable(this.getResources().getDrawable(R.drawable.unshape_textview));
                break;
            case 3:
                if (peixun_quanbuFragment == null) {
                    peixun_quanbuFragment = new Peixun_QuanbuFragment();
                    transaction.add(R.id.peixun_FrameLayout, peixun_quanbuFragment);
                } else {
                    transaction.show(peixun_quanbuFragment);

                }
                quanbu.setTextColor(this.getResources().getColor(R.color.green));
                quanbu.setBackgroundDrawable(this.getResources().getDrawable(R.drawable.unshape_textview));
                break;
        }
        transaction.commit();
    }

    private void hideFragment(FragmentTransaction transaction)
    {
        if (peixun_rumenFragment != null)
        {
            transaction.hide(peixun_rumenFragment);
        }
        if (peixun_cainiaoFragment != null)
        {
            transaction.hide(peixun_cainiaoFragment);
        }
        if (peixun_jinjieFragment != null)
        {
            transaction.hide(peixun_jinjieFragment);
        }
        if (peixun_quanbuFragment != null)
        {
            transaction.hide(peixun_quanbuFragment);
        }

    }

public  void color(){

    rumen.setTextColor(this.getResources().getColor(R.color.white));
    rumen.setBackgroundDrawable(this.getResources().getDrawable(R.drawable.shape_textview));
    jinjie.setTextColor(this.getResources().getColor(R.color.white));
    jinjie.setBackgroundDrawable(this.getResources().getDrawable(R.drawable.shape_textview));
    cainiao.setTextColor(this.getResources().getColor(R.color.white));
    cainiao.setBackgroundDrawable(this.getResources().getDrawable(R.drawable.shape_textview));
    quanbu.setTextColor(this.getResources().getColor(R.color.white));
    quanbu.setBackgroundDrawable(this.getResources().getDrawable(R.drawable.shape_textview));
}

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rumen:
                setSelect(0);
                break;
            case R.id.cainiao:
                setSelect(1);
                break;
            case R.id.jinjie:
                setSelect(2);
                break;
            case R.id.quanbu:
                setSelect(3);
                break;
            default:
                break;
        }
    }


    private  void  lunbotu(){

        //轮播图
        viewList = new ArrayList<View>();
        for (int i = 0; i < imgs.length; i++) {
            ImageView image = new ImageView(getActivity());
            image.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            //设置显示格式
            image.setScaleType(ImageView.ScaleType.CENTER_CROP);
            image.setImageResource(imgs[i]);
            viewList.add(image);
        }
        bannerView.startLoop(true);
        bannerView.setViewList(viewList);
       bannerView.setTransformAnim(true);

    }






}

